package com.example.nsl.behavior;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 右边试图的偏移受制于左边视图的影响，不需要单独处理了
 */
public class IamgeDeyBavior extends ViewOffsetBehavior {

    public IamgeDeyBavior(){
    }

    public IamgeDeyBavior(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void layoutChild(@NonNull CoordinatorLayout parent, @NonNull View child, int layoutDirection) {
//        super.layoutChild(parent, child, layoutDirection);
//        Rect rect=new Rect();
        for (int i=0; i<parent.getChildCount(); i++){
            View view=parent.getChildAt(i);

            if(view instanceof RecyclerView){
                RecyclerView childrecyclerview= (RecyclerView) view;
                int [] post=new int[2];
                childrecyclerview.getLocationOnScreen(post);

                Rect rectImage=new Rect();

                rectImage.left= childrecyclerview.getMeasuredWidth();

                rectImage.top=post[1];
                if (ViewCompat.getFitsSystemWindows(parent)) {
                    @SuppressLint("RestrictedApi")
                    final WindowInsetsCompat parentInsets = parent.getLastWindowInsets();
                    rectImage.top= rectImage.top+ parentInsets.getSystemWindowInsetTop()
                            + parentInsets.getSystemWindowInsetBottom()+(childrecyclerview.getMeasuredHeight()-child.getMeasuredHeight())/2;
                }else{
                    rectImage.top= rectImage.top+(childrecyclerview.getMeasuredHeight()-child.getMeasuredHeight())/2;
                }
                rectImage.right=child.getMeasuredWidth()+rectImage.left;
                rectImage.bottom=child.getMeasuredHeight()+rectImage.top;
                //int l, int t, int r, int b
                child.layout(rectImage.left,rectImage.top,rectImage.right,rectImage.bottom);


                Log.e("ImageBehavior","" + post[0]+ "_" +post[1] + rectImage +"  childleft : "+child.getLeft() );
                break;
            }
        }
    }


    @Override
    public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
        return  dependency instanceof RecyclerView;
    }


    @Override
    public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
        int maxLeft=parent.getMeasuredWidth(); // 最大的的偏移距离呢                           maxLeft:1080
        int minLeft=parent.getMeasuredWidth()-child.getMeasuredWidth(); // 最小的偏移距离呢   minLeft:702
        int dependencyLeft=dependency.getLeft(); //当前dependencyView已经偏移的距离
        if(dependencyLeft <= 0){
            int nextLeft = 0;
            if(-child.getMeasuredWidth() <= dependencyLeft){
                nextLeft=  maxLeft + dependencyLeft;
            }else {
                nextLeft=  maxLeft -child.getMeasuredWidth();
            }
            if( minLeft <= nextLeft &&  nextLeft<= maxLeft){
                child.setLeft(nextLeft);
            }
        }else { //最大值的时候，恢复当前状态
            child.setLeft(maxLeft);
        }
        return true;
    }
}
